<?php

class View
{
	/**
	 * get views
	 * @var View
	 */
	static protected $_instance = null;

	/**
	 * @var array
	 */
	static protected $_paths = array(NUO_APP_PATH);

	/**
	 * get instance
	 * @return View
	 */
	static public function getInstance()
	{
		if (null === self::$_instance)
		{
			self::$_instance = new self;
		}
		return self::$_instance;
	}

	/**
	 * @param View_Parser $parser
	 * @return void
	 */
	static public function setParser($parser)
	{
		self::$_parser = $parser;
	}

	/**
	 * find a path
	 * @param string $script
	 * @param bool $include
	 * @param array $paths
	 * @return string
	 */
	static public function findScript($script, $include = 1, $paths = null)
	{

		if (false == $include)
		{
			if (file_exists($script))
			{
				return $script;
			}
			return null;
		}

		if (null == $paths)
		{
			$paths = self::$_paths;
		}

		foreach ($paths as $path)
		{
			if (file_exists($file = $path . '/' . $script))
			{
				return $file;
			}
		}

		return null;
	}

	/**
	 * @param array $arrayOfData
	 * @param string $scriptFileName
	 * @param bool $findInPath
	 * @param array $includePaths
	 * @return string
	 */
	static public function render($arrayOfData, $scriptFileName, $findInPath = 1, $includePaths = null)
	{

		ob_start();

		$_file = self::findScript($scriptFileName, $findInPath, $includePaths);

		if (null == $_file)
		{
			throw new Exception("$scriptFileName not found in view paths.");
		}

		extract($arrayOfData, EXTR_SKIP);
		
		$l18n = Nuo::getHelper('@l18n');

		include $_file;

		return ob_get_clean();
	}

}
